package com.qf.team2.netdisc.service.impl;

import com.qf.team2.netdisc.common.exception.BusinessException;
import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.entity.User;
import com.qf.team2.netdisc.common.util.MD5Util;
import com.qf.team2.netdisc.dao.DocumentUploadMapper;
import com.qf.team2.netdisc.service.DocumentUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 文件上传服务接口实现类
 *
 * @author Simon_Wong
 * @date 2021/05/26 10:56
 */
@Service
public class DocumentUploadServiceImpl implements DocumentUploadService {
    private final int CAPACITY_OF_COMMON_USER = 1024 * 1024;
    private final int CAPACITY_OF_VIP_USER = 1024 * 1024;
    @Autowired
    private DocumentUploadMapper documentUploadMapper;

    @Override
    public boolean checkUserCapacity(Integer id, Long fileSize, MultipartFile file) {
        if (file == null || file.getOriginalFilename() == null) {
            throw new BusinessException("请重新选择文件，你选择的文件为空");
        }
        User user = documentUploadMapper.selectDocumentById(id);
        if (user.getCapacity() + fileSize > CAPACITY_OF_COMMON_USER) {
            throw new BusinessException("用户容量不足,请充值");
        }
        return true;
    }



    @Override
    public Document checkFileUpload(String filePath, MultipartFile file, Long fileSize, Integer userId) {
        System.out.println("文件:" + filePath);
        String files = file.getOriginalFilename();
        String urls = filePath;
        urls=filePath.replace("\\","\\\\");
        System.out.println("url:" + urls);
        /*if (file.isEmpty()) {
            throw new BusinessException("文件上传失败");
        }*/
        String filePaths = filePath +"\\" +file.getOriginalFilename();
        System.out.println("upload:" + filePaths);
        File saveFile = new File(filePaths);
        System.out.println(saveFile);
        try {
            file.transferTo(saveFile);
            String md5 = MD5Util.md5(saveFile);
            System.out.println();
            System.out.println("urls:"+urls);
            urls=urls+"\\\\"+file.getOriginalFilename();
            System.out.println("urls:"+urls);
            Document document = documentUploadMapper.insertFile(files, fileSize, urls, userId, 0, 0, md5);
            updateParentFileSize(fileSize, filePath, userId);
            User user = documentUploadMapper.updateCapacityById(userId, fileSize);
            System.out.println(user);
            return document;
        } catch (IOException e) {
            throw new BusinessException("文件上传失败");
        }
    }

    @Override
    public Integer updateParentFileSize(Long size, String url, Integer userId) {

        String urls =url;
        String names;
        System.out.println("mark");
        System.out.println(urls);
        urls = urls + "\\";
        while (!"".equals(urls)) {
            urls=urls.replace("\\","\\\\");

            //修改父级的容量
            documentUploadMapper.updateById(size, urls, userId);

            String[] temp=urls.split("\\\\");
            names=(temp[temp.length - 1])+"\\\\";

            urls = urls.replace(names,"");
            urls = urls.replace("\\\\","\\");
            try {
                Thread.sleep(500L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public boolean checkFileName(String url, Integer userId, String name) {
        String urls =url;
        urls=urls.replace("\\","\\\\");
        System.out.println("url:" + urls);
        List<Document> documents = documentUploadMapper.selectDocumentByIdAndUserId(urls, userId);
        documents.forEach(System.out::println);
        for (Document document : documents) {
            if (document.getName().equals(name)) {
                throw new BusinessException("文件名已存在");
            }
        }
        return true;
    }

}
